const {isEqual} = require('./util');
const update = async (json,doc,editableFields) => {
    const _this = doc;
    editableFields.forEach( (key) => {
        let to = json[key],
            from = _this.get(key) || '';
        if( !isEqual(from,to) ){
            _this.set( key, to );
        }
    })
    _this.updateTime = Date.now();
    try{
        const ret = await _this.save();   
        return ret;
    }catch(err){
        throw {code:102, error:'Update failed.Please try again.'};
    }
}

module.exports = {
    update
}